#pragma warning(disable:4996)

#include "Timer.h"

#include <windows.h>
/*
  time() : 1s
  clock() : 1ms ?
  timeGetTime() : 10ms
  GetTickCount() : 10ms 
  QueryPerformanceCounter() : 1us

*/
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CTimer::CTimer(void)
{
	Reset();
	m_fCumulativeTime = 0;	

	LARGE_INTEGER i;
	QueryPerformanceFrequency(&i);
	m_Freq = (double)i.QuadPart;
}

CTimer::~CTimer(void)
{
}

void CTimer::Reset()
{
	m_fCumulativeTime = 0;
	m_fElapsedTime = 0;
	m_nCount = 0;
}

void CTimer::Start()
{	
	//time (& m_Start);     
	//m_Start = clock();	
	LARGE_INTEGER i;
	QueryPerformanceCounter(&i);
	m_Start = (double)i.QuadPart;
	
}

void CTimer::Stop()
{
	/*
	time (& m_fEnd);
	m_fElapsedTime = difftime (m_fEnd, m_fStart);	
	*/
	/*
	m_End = clock();
	m_fElapsedTime = (float)(m_End-m_Start) / CLOCKS_PER_SEC;
	*/
	
	LARGE_INTEGER i;
	QueryPerformanceCounter(&i);
	m_End = (double)i.QuadPart;
		
	m_fElapsedTime = (m_End - m_Start) / m_Freq;		
	m_fCumulativeTime += m_fElapsedTime;
	m_nCount++;
}

double CTimer::GetCumulativeTime()
{
	return m_fCumulativeTime;
}

double CTimer::GetElapsedTime()
{
	return m_fElapsedTime;
}

double CTimer::GetAverageTime()
{
	return m_fCumulativeTime/m_nCount;
}

void  CTimer::Show(ofstream &fout, char *timer)
{
	char line[1000];
	sprintf(line, "%10s :  time = %.3f  n = %4d  aver = %.4f\n", timer, GetCumulativeTime(), GetCount(), GetAverageTime());
	fout << line;
	//fout << timer << " : " << "    time = " << GetCumulativeTime() << "    n    = " << GetCount() << "    aver = " << GetAverageTime() << endl;	
}
